package com.shenyue.nbtownlet.location;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.Toast;

import java.util.List;

/**
 * GPS定位
 */

public class BDLocationManager {
    private LocationManager locationManager;
    private Location location;
    private Context mContext;

    public BDLocationManager() {
    }

    public void start(Context context) {
        mContext = context;
        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }

        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        List<String> providers = locationManager.getProviders(true);
        if (providers.contains(LocationManager.GPS_PROVIDER)) {
            //如果是GPS
            String locationProvider = LocationManager.GPS_PROVIDER;
            //监视地理位置变化
            locationManager.requestLocationUpdates(locationProvider, 1000, 0f, gpsLocationListener);
        }
        if (providers.contains(LocationManager.NETWORK_PROVIDER) || providers.contains(LocationManager.PASSIVE_PROVIDER)) {
            //如果是Network
            String locationProvider = LocationManager.NETWORK_PROVIDER;
            //监视地理位置变化
            locationManager.requestLocationUpdates(locationProvider, 2000, 0f, networkLocationlistener);
        }

        if (providers.contains(LocationManager.PASSIVE_PROVIDER)) {
            //如果是Passive
            String locationProvider = LocationManager.PASSIVE_PROVIDER;
            //监视地理位置变化
            locationManager.requestLocationUpdates(locationProvider, 3000, 0f, passiveLocationlistener);
        }
    }

    public void stop() {
        if (locationManager != null) {
            locationManager.removeUpdates(gpsLocationListener);
            locationManager.removeUpdates(networkLocationlistener);
            locationManager.removeUpdates(passiveLocationlistener);
        }
        locationManager = null;
    }

    private void showLocation(Location location) {
        Log.d("定位成功------->", "location------>" + "经度为：" + location.getLatitude() + "\n纬度为" + location.getLatitude());
        this.location = location;
    }

    public Location getLocation() {
        try {
            if (locationManager == null)
                return null;
            if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return null;
            }
            if (location == null)
                location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (location == null)
                location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (location == null)
                location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
            return location;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * LocationListern监听器
     * 参数：地理位置提供器、监听位置变化的时间间隔、位置变化的距离间隔、LocationListener监听器
     */
    private LocationListener gpsLocationListener = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle arg2) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }

        @Override
        public void onLocationChanged(Location location) {
            if (location != null)
                showLocation(location);
        }
    };
    /**
     * LocationListern监听器
     * 参数：地理位置提供器、监听位置变化的时间间隔、位置变化的距离间隔、LocationListener监听器
     */
    private LocationListener passiveLocationlistener = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle arg2) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }

        @Override
        public void onLocationChanged(Location location) {
            if (location != null)
                showLocation(location);
        }
    };

    /**
     * LocationListern监听器
     * 参数：地理位置提供器、监听位置变化的时间间隔、位置变化的距离间隔、LocationListener监听器
     */
    private LocationListener networkLocationlistener = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle arg2) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }

        @Override
        public void onLocationChanged(Location location) {
            if (location != null)
                showLocation(location);
        }
    };
}
